﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DAL;
using CMSModel;

namespace BLL
{
    /// <summary>
    ///  公司概况导入
    /// </summary>
    public class CompanyOverviewImport
    {
        public void Import()
        {
            CompanyOverviewDAL c = new CompanyOverviewDAL();
            var tb=  c.GetAll();
            var db = DBManager.GetCmsEmtity();
             for (int i = 0; i < tb.Rows.Count; i++)
             {
                 CompanyOverview t = null;
                 var row = tb.Rows[i];
                 string stockId=Tools.GetString(row["STOCK_ID"]);
                 var flagAdd = false;
                 if (db.CompanyOverview.Any(x => x.StockId == stockId))
                 {
                     t = db.CompanyOverview.First(x => x.StockId == stockId);
                 }
                 else
                 {
                     t = new CompanyOverview();
                     flagAdd = true;
                 }
                 CommonBll.SetValue(t, "StockId", Tools.GetString(row["STOCK_ID"]));
                 CommonBll.SetValue(t, "StockName", Tools.GetString(row["STOCK_NAME"]));
                 CommonBll.SetValue(t,"BlockName",Tools.GetString(row["BLOCK_NAME"]));
                 CommonBll.SetValue(t,"Reion",Tools.GetString(row["REGION_ID"]));
                 int? temp = Tools.GetInt(row["list_date"]);
                 DateTime? tempTime=null;
                 if (temp != null)
                 {
                     string dd = (temp % 100).ToString();
                     temp = temp / 100;
                     string MM = (temp % 100).ToString();
                     string yyyy = (temp / 100).ToString();
                     tempTime = Convert.ToDateTime(yyyy + "-" + MM + "-" + dd);
                 }
                 CommonBll.SetValue(t,"list_date",tempTime);
                 CommonBll.SetValue(t,"net_asset",Tools.GetDecimal(row["net_asset"]));
                 CommonBll.SetValue(t,"cash_flow",Tools.GetDecimal(row["cash_flow"]));
                 CommonBll.SetValue(t,"accumulation_fund",Tools.GetDecimal(row["accumulation_fund"]));
                 CommonBll.SetValue(t,"ROE",Tools.GetDecimal(row["ROE"]));
                 CommonBll.SetValue(t,"net_profit_growth",Tools.GetDecimal(row["net_profit_growth"]));
                 CommonBll.SetValue(t,"unappropriated_profit",Tools.GetDecimal(row["unappropriated_profit"]));
                 CommonBll.SetValue(t, "business_income", Tools.GetDecimal(row["business_income"]));
                 CommonBll.SetValue(t, "total_share", Tools.GetDecimal(row["total_share"]));
                 CommonBll.SetValue(t, "flow_share", Tools.GetDecimal(row["flow_share"]));
                 CommonBll.SetValue(t, "EPS", Tools.GetDecimal(row["EPS"]));
                 if (flagAdd)
                 {
                     db.CompanyOverview.AddObject(t);
                 }
                 db.SaveChanges();
             }
          
            
        }
    }
}